package com.newdriver.edu.inf.manager.service;

import com.newdriver.edu.inf.manager.dao.Dao;
import com.newdriver.edu.inf.manager.domain.Student;

//业务员
public class Service {
    private Dao dao = new Dao();
    public boolean addStudent(Student stu) {
        //创建Dao
        //Dao dao = new Dao();
        //传递Dao 调用addStudent
        return dao.addStuddent(stu);
        //将返回的boolean返回给客服
    }

    public boolean isExists(String id) {
        //Dao dao = new Dao();
        Student[] stus = dao.findAllStudent();

        //假设id不存在
        boolean flag = false;
        //遍历数组进行判断
        for (int i = 0; i < stus.length; i++) {
            Student student = stus[i];
            if (student != null && student.getId().equals(id)) {
                flag = true;
                break;
            }
        }
        return flag;
    }

    public Student[] findAllStudent() {
        //获取对象数组 findall
        Student[] allStudent = dao.findAllStudent();
        //判断数组是否有信息 数组中只要有一个元素不是null 说明有学生信息
        boolean flag=false;
        for (int i = 0; i < allStudent.length; i++) {
            Student stu = allStudent[i];
            if (stu!=null) {
                flag=true;
                break;
            }
        }
        if (flag) {
            return allStudent;
        }else{
            return null;
        }
    }

    public void delStuById(String delId) {
        dao.delStuById(delId);
    }


    public void updateStudent(String updateId, Student newStudent) {
        dao.updateStudent(updateId,newStudent);
    }
}